import message from "./message.js";
import store from './store.js';


let isCloud = false;
// let baseURL = "https://www.chuyin.site:8010/wx";

let baseURL = "http://localhost:8010/wx";


if (isCloud) {
	baseURL = "/container-fax/fax";
	var c1 = new wx.cloud.Cloud({
		resourceEnv: 'kerqinserver-5gyuxi1ecf8c0a66'
	});
	c1.init();
}

const httpObject = {
	login: function(code) {
		return httpObject.get({
			url: "/login/jscode",
			param: {
				code
			}
		}).then(data => {
			store.commit("loginSuccess", data);
			uni.hideLoading();
		});
	},
	_changeParam: param => {
		param.data = param.param;
		param.url = baseURL + param.url;
		return param;
	},
	put: param => {
		param.method = "PUT";
		return httpObject._request(httpObject._changeParam(param));
	},
	get: param => {
		param.method = "GET";
		return httpObject._request(httpObject._changeParam(param));
	},
	post: param => {
		param.method = "POST";
		return httpObject._request(httpObject._changeParam(param));
	},
	"delete": param => {
		param.method = "DELETE";
		return httpObject._request(httpObject._changeParam(param));
	},

	_request: param => {
		return new Promise((resolve, reject) => {
			if (isCloud) {} else {
				let localHeader = {
					'content-type': 'application/x-www-form-urlencoded',
				};

				if (store.state.sessionId) {
					localHeader["Cookie"] = "JSESSIONID=" + store.state.sessionId;
				}

				let requestOption = {
					url: param.url,
					method: param.method,
					data: param.data,
					withCredentials: true,
					header: localHeader,
					fail: function(f) {
						uni.hideLoading();
						message.error("连接服务器失败！");
						reject({
							STATUS: "fail",
							data: param
						});
					},
					error: function(e) {
						uni.hideLoading();
						console.log("网络错误，无法连接到服务器！");
						message.error("网络错误，无法连接到服务器！");
						reject({
							STATUS: "error",
							data: param
						});
					},
					success: function(res) {
						let cookieSet = res.header['Set-Cookie'];

						if (cookieSet) {
							let jSessionId = cookieSet.split(";")[0].split("=")[1];

							if (jSessionId) {
								store.commit("setSessionId", jSessionId);
							}
						}
						let response = res.data;
						if (response.STATUS === "SUCCESS") {
							uni.hideLoading();
							resolve(response.DATA);
						} else if (response.STATUS === "FAIL") {
							uni.hideLoading();
							message.warning(response.MESSAGE);
							reject({
								STATUS: "FAIL",
								data: {
									message: response.MESSAGE
								}
							});
						} else if (response.STATUS === "NOT_LOGIN") {

							store.commit("setRequestOption", requestOption);

							uni.login({
								success: function(res) {
									httpObject.login(res.code);
								}
							});
						} else if (response.STATUS === "BIND_FAIL") {
							uni.hideLoading();
							let openId = response.MESSAGE;
							store.commit("saveOpenId", openId)
							reject({
								STATUS: "BIND_FAIL",
								data: {
									openId
								}
							});
						} else if (response.STATUS === "ERROR") {
							uni.hideLoading();
							message.error(response.MESSAGE);
							reject({
								STATUS: "ERROR",
								data: {
									message: response.MESSAGE
								}
							});
						} else {
							uni.hideLoading();
							message.error("系统出现未知错误！");
							reject({
								STATUS: "ERROR",
								data: {
									message: "系统出现未知错误！"
								}
							});
						}
					}
				};
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				uni.request(requestOption);

			}
			
		});
	}
}

export default httpObject;
